<?php
/**
 * 定时检测订单预警时间
 * Created by PhpStorm.
 * User: poptser
 * Email: poptser@foxmail.com
 * Date: 2016/8/25
 * Time: 16:52
 */

namespace YMApi\Controller;
use Think\Controller;
use Think\Model;

class GoodsShelfLifeCheckController extends Controller
{
    public $pagesize=1000;
    public function check(){
        $this->goodsshelflife();//检查商品保质期是否过期
    }
    
     /*
     * 插入系统工作日志
     * */
    private function goods_shelf_life_log($data){
        $data_arr=array(
            'wid'=>$data['wid'],
            'goods_id'=>$data['goods_id'],
            'goods_sn'=>$data['goods_sn'],
            'goods_name'=>$data['goods_name'],
            'procurement_sn'=>$data['procurement_sn'],
            'creation_date'=>$data['creation_date'],
            'shelf_life'=>$data['shelf_life'],
            'log_type'=>$data['log_type']
        );
        M('goods_shelf_life_log')->add($data_arr);
    }
    
   /*
    * 检查系统工作日志是否已经存在
    */
    private function check_repeat_goods_shelf_life__log($wid,$source_no,$goods_id){
        $id=M('goods_shelf_life_log')->where(array('wid'=>$wid,'procurement_sn'=>$source_no,'goods_id'=>$goods_id))->getField('id');
        if($id>0){
            return false;
        }else{
            return true;
        }
    }
    
    /*
     * 商品保质期预警
     * */
     private function goodsshelflife(){
        ini_set('max_execution_time', '0');
        set_time_limit(0);
        //ini_set('memory_limit','4096M');
        $page=S('goodsshelflife_page');
        if(empty($page)||!is_numeric($page)){
            S('goodsshelflife_page',1,86400);
            $page=1;
        }
        $sql="delete from ".C ( 'DB_PREFIX' )."goods_shelf_life_log where DATEDIFF(from_unixtime(shelf_life,'%Y-%m-%d'),CURDATE())<-8 or (DATEDIFF(from_unixtime(shelf_life,'%Y-%m-%d'),CURDATE())<0 and log_type=1)";
        M()->execute($sql);
        $begin_num = ($page-1)*$this->pagesize;
        $warehouse_procurement_mod=M('warehouse_procurement');
        $count=$warehouse_procurement_mod->count();
        $all_page = ceil($count/$this->pagesize);
        $wplist=$warehouse_procurement_mod->field('id,procurement_sn')
                   ->limit($begin_num.",".$this->pagesize)
                   ->select();
        $warehouse_procurement_detail_mod=M('warehouse_procurement_detail');
        if($wplist){
            foreach ($wplist as $key=>$val){
                    $wpdlist=$warehouse_procurement_detail_mod->alias('wpd')
                             ->field('wpd.procurement_id,wpd.wid,g.goods_id,g.goods_sn,g.goods_name,g.expiration_date,wpd.production_date')
                             ->join(C ( 'DB_PREFIX' ).'goods as g on g.goods_id=wpd.goods_id')
                             ->where(array('procurement_id'=>$val['id']))
                             ->select();
                    if($wpdlist){
                        $time=time();
                        foreach ($wpdlist as $k=>$v){
                            if(!$this->check_repeat_goods_shelf_life__log($v['wid'], $val['procurement_sn'], $v['goods_id'])){
                                continue;
                            }
                            if($v['production_date']){
                                 $day_date=date('Y-m-d',$time);
                                 $production_date=date('Y-m-d',$v['production_date']);
                                 $expiration_date=date('Y-m-d',strtotime('+'.$v['expiration_date'].' day '.$production_date));
                                 echo 'sn=>'.$val['procurement_sn'].'=>goods_id=>'.$v['goods_id'].'=>production_date=>'.date('Y-m-d',$v['production_date']).'=>day=>'.$v['expiration_date'].'=>expiration_date=>'.$expiration_date.'<br />';
                                 $d1 = strtotime($day_date);//当前日期 
                                 $d2 = strtotime($expiration_date);//保质期限
                                 $shelf_life=round(($d2-$d1)/3600/24);
                                 
                                 $data=array(
                                                'wid'=>$v['wid'],
                                                'goods_id'=>$v['goods_id'],
                                                'goods_sn'=>$v['goods_sn'],
                                                'goods_name'=>$v['goods_name'],
                                                'procurement_sn'=>$val['procurement_sn'],
                                                'creation_date'=>$v['production_date'],
                                                'shelf_life'=>$d2,
                                            );
                                 if($shelf_life>=0 && $shelf_life<=5){ //即将到期
                                     $data['log_type']=1;
                                 }elseif ($shelf_life < 0 && $shelf_life >= -7){ //已经过期
                                     $data['log_type']=2;
                                 }else{
                                     unset($data);
                                     continue;
                                 }
                                 
                                 $this->goods_shelf_life_log($data);
                            }
                        }
                    }
            }
        }
        $next_page = $page+1;
        S('goodsshelflife_page',$next_page,86400);
        if($next_page>$all_page){
            S('goodsshelflife_page',1,86400);
        }
  }
    
}